
import pymysql
class DataBase:
    def __init__(self,host,user,password,database):
        self.host=host
        self.user=user
        self.password=password
        self.database=database    #要连接的库名
        self.port=3306
        self.charset='utf8'
        
    def insert(self,sql):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,     #要连接的库名
            port=self.port,
            charset=self.charset
		)
        cursor = db.cursor()
        cursor.execute(sql)
        db.commit()
        cursor.close()
        db.close()
class human:
    def __init__(self,username:str,age:int,sex:str,high:float):
            self.username=username
            self.age=age
            self.sex=sex
            self.high=high
    def __str__(self):#str函数是用来告诉编辑器如何打印类的对象的
        return f"姓名: {self.username}, 年龄: {self.age}, 性别: {self.sex}, 身高: {self.high}cm"

R1 = human('li',16,'男',198.2)
R2 = human('233',19,'男',170.2)
R3 = human('lsif',16,'男',188.2)
R4 = human('张三',15,'男',198.2)
list = [R1,R2,R3,R4]
for i in list:
     print(i)
DB = DataBase('localhost','root','root','human')
for i in list:
    DB.insert(f"insert into humann values ('{i.username}',{i.age},'{i.sex}',{i.high})")

